import apiClient from './api';
import type {
  NetworkDetail,
  NetworkSummary,
  ListResponse,
  ApiResponse,
} from '../types/api';

/**
 * ��Q�h
 * @param connection ޥID
 */
export const listNetworks = async (
  connection: string
): Promise<ListResponse<NetworkSummary>> => {
  const response = await apiClient.get<ListResponse<NetworkSummary>>('/networks', {
    params: { connection },
  });
  return response.data;
};

/**
 * ��Q���
 * @param connection ޥID
 * @param name Q�
�
 */
export const getNetworkDetail = async (
  connection: string,
  name: string
): Promise<NetworkDetail> => {
  const response = await apiClient.get<NetworkDetail>('/network', {
    params: { connection, name },
  });
  return response.data;
};

/**
 * �Q�
 * @param connection ޥID
 * @param xmlConfig Q� XML Mn
 */
export const createNetwork = async (
  connection: string,
  xmlConfig: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/networks', xmlConfig, {
    params: { connection },
    headers: { 'Content-Type': 'text/xml' },
  });
  return response.data;
};

/**
 *  dQ�
 * @param connection ޥID
 * @param name Q�
�
 */
export const deleteNetwork = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.delete<ApiResponse>('/network', {
    params: { connection, name },
  });
  return response.data;
};

/**
 * /�Q�
 * @param connection ޥID
 * @param name Q�
�
 */
export const startNetwork = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/network/start', null, {
    params: { connection, name },
  });
  return response.data;
};

/**
 * \bQ�
 * @param connection ޥID
 * @param name Q�
�
 */
export const stopNetwork = async (
  connection: string,
  name: string
): Promise<ApiResponse> => {
  const response = await apiClient.post<ApiResponse>('/network/stop', null, {
    params: { connection, name },
  });
  return response.data;
};

/**
 * ��Q� XML Mn
 * @param connection ޥID
 * @param name Q�
�
 */
export const getNetworkXML = async (
  connection: string,
  name: string
): Promise<string> => {
  const response = await apiClient.get<string>('/network/xml', {
    params: { connection, name },
  });
  return response.data;
};

	
export const networkService = {
  listNetworks,
  getNetworkDetail,
  createNetwork,
  deleteNetwork,
  startNetwork,
  stopNetwork,
  getNetworkXML,
};